// addressPackage/myAddress.js
import request from '../../../utils/request'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 若为1，则是下单页面转来
    flag: 0,
    userInfo: {},
    authorization: '',
    addressList: []
  },

  /**
   * 添加地址按钮
   */
  insertAddress() {
    wx.navigateTo({
      url: '/addressPackage/pages/addMyAddress/addMyAddress',
    })
  },

  selectAddress(options) {
    let flag = this.data.flag;
    if (flag == 1) {
      let pages = getCurrentPages()
      let confirmPage;
      for (let index in pages) {
        console.log(pages[index])
        if (pages[index].route == 'orderPackage/pages/confirmOrder/confirmOrder') {
          confirmPage = pages[index]
          break;
        }
      }
      confirmPage.setData({
        addressInfo: options.currentTarget.dataset.item
      })
      // 传送id，返回页面
      wx.navigateBack({
        url: '/orderPackage/pages/confirmOrder/confirmOrder'
      })
    }
  },

  /**
   * 点击其中一个地址展示数据 (方便修改)
   */
  findAddress(data) {
    const id = data.currentTarget.dataset.id
    wx.navigateTo({
      url: '/addressPackage/pages/addMyAddress/addMyAddress?addressId=' + id
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function(options) {
    let that = this;
    // 下订单进入我的地址标志
    if (options == null) {

    } else if (options.flag == 1) {
      that.setData({
        flag: options.flag
      })
    }
    // 获取地址列表
    let result = await request('/address/list');
    this.data.addressList = result.data
    that.setData({
      addressList: that.data.addressList
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.onLoad();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})